package ntut.andengine.gamedesign;

import javax.microedition.khronos.opengles.GL10;

import ntut.andengine.physics.ContactEvent;
import ntut.andengine.physics.PhysicalComponent;
import ntut.andengine.physics.PhysicalComponent.ComponentColor;
import ntut.andengine.physics.PhysicalFactory;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.entity.modifier.LoopEntityModifier;
import org.anddev.andengine.entity.modifier.RotationModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.util.GLHelper;

import android.app.Activity;
import android.content.Context;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;

public class GameSceneLevel1 extends GameSceneLevel{

	// ===========================================================
	// Constants
	// ===========================================================
	
	// ===========================================================
	// Fields
	// ===========================================================
	
	TextureRegion mLightTextureRegion;
	TextureRegion mPhoneTextureRegion;
	
	// ===========================================================
	// Constructors
	// ===========================================================

	public GameSceneLevel1(Context context, Engine engine) {
		super(context, engine);
	}
	
	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	@Override
	public void onLoadResources() {
		super.onLoadResources();
		
		this.mBitmapTexture = new BitmapTexture(128, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		this.mLightTextureRegion = BitmapTextureRegionFactory.createFromAsset(this.mBitmapTexture, mContext, "particle_point.png", 0, 0);
		this.mPhoneTextureRegion = BitmapTextureRegionFactory.createFromAsset(this.mBitmapTexture, mContext, "phone.png", 32, 0);
		
		mEngine.getTextureManager().loadTexture(this.mBitmapTexture);	
	}

	@Override
	public Scene onLoadScene() {
		((MainGameActivity) mContext).setGameSceneLevel(this);
		
		this.mScene.setOnSceneTouchListener(this);

//		PhysicalFactory.CreateWall(this, new Vector2(200, 140), 400, 40, ComponentColor.White);
//        PhysicalFactory.CreateWall(this, new Vector2(280, 260), 400, 40, ComponentColor.White);
//        PhysicalFactory.CreateWall(this, new Vector2(40, 380), 80, 40, ComponentColor.White);
//        PhysicalFactory.CreateWall(this, new Vector2(120, 500), 80, 40, ComponentColor.White);
//        PhysicalFactory.CreateWall(this, new Vector2(40, 620), 80, 40, ComponentColor.White);
//        PhysicalFactory.CreateWall(this, new Vector2(120, 700), 80, 40, ComponentColor.White);
//        PhysicalFactory.CreateWall(this, new Vector2(180, 500), 40, 440, ComponentColor.White);
//        PhysicalFactory.CreateWall(this, new Vector2(300, 740), 40, 120, ComponentColor.White);
//        PhysicalFactory.CreateWall(this, new Vector2(420, 640), 40, 160, ComponentColor.White);
//        PhysicalFactory.CreateWall(this, new Vector2(300, 420), 40, 120, ComponentColor.White);
//        PhysicalFactory.CreateWall(this, new Vector2(300, 580), 200, 40, ComponentColor.White);
//        PhysicalFactory.CreateWall(this, new Vector2(400, 460), 160, 40, ComponentColor.White);
//        
//        PhysicalComponent gate = PhysicalFactory.CreateTransmissionGate(this, new Vector2(400, 400));
//        gate.getBody().setUserData(new ContactEvent("Gate"){
//
//			@Override
//			public void beginContact(Body body, Vector2 Normal) {
//				
//				// LEVEL COMPLETE!!
//				if(body.getUserData().toString() == "Player"){
//					//changeScene(mBitmapTexture, new GameSceneLevel2(mContext, mEngine));
//					MainGameActivity.startActivity((Activity) mContext, GameSceneLevel2.class);
//				}
//			
//			}
//
//			@Override
//			public void endContact(Body body, Vector2 Normal) {
//				// TODO Auto-generated method stub
//				
//			}
//		
//		});
		
	// hint
		final Sprite light = new Sprite(400, 50, this.mLightTextureRegion);
		light.setScale(5);
		light.setColor(1, 0.824f, 0.234f);
		
		final Sprite phone = new Sprite(400, 30, this.mPhoneTextureRegion) {
			@Override
			protected void applyRotation(final GL10 pGL) {
				/* Disable culling so we can see the backside of this sprite. */
				GLHelper.disableCulling(pGL);

				final float rotation = this.mRotation;

				if(rotation != 0) {
					final float rotationCenterX = this.mRotationCenterX;
					final float rotationCenterY = this.mRotationCenterY;

					pGL.glTranslatef(rotationCenterX, rotationCenterY, 0);
					pGL.glRotatef(rotation, 0.5f, 1, 0);
					pGL.glRotatef(rotation, 0, 1, 0);
					pGL.glTranslatef(-rotationCenterX, -rotationCenterY, 0);

				}
			}

			@Override
			protected void drawVertices(final GL10 pGL, final Camera pCamera) {
				super.drawVertices(pGL, pCamera);

				/* Enable culling as 'normal' entities profit from culling. */
				GLHelper.enableCulling(pGL);
			}
		};
		phone.registerEntityModifier(new LoopEntityModifier(new RotationModifier(3, -40, 40)));
		
		mScene.attachChild(light);
		mScene.attachChild(phone);
	// hint
        
        //mSparkle = PhysicalFactory.CreateSparkle(this, new Vector2(40, 40), ComponentColor.Red);

		this.mScene.registerUpdateHandler(this.mPhysicsWorld);		

		
		return mScene;
	}

	// ===========================================================
	// Methods
	// ===========================================================
	
	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
	
}
